#-*- coding: utf-8 -*-

import datetime
import re

from django.db import models
from django.contrib import admin

class ComDomainManager(models.Manager):
    """
    .com域名管理
    """
    def get_query_set(self):
        return super(ComDomainManager,self).get_query_set().filter(region = 0)

class NetDomainManager(models.Manager):
    """
    .com域名管理
    """
    def get_query_set(self):
        return super(NetDomainManager,self).get_query_set().filter(region = 1)

class OrgDomainManager(models.Manager):
    """
    .com域名管理
    """
    def get_query_set(self):
        return super(OrgDomainManager,self).get_query_set().filter(region = 2)

class InfoDomainManager(models.Manager):
    """
    .com域名管理
    """
    def get_query_set(self):
        return super(InfoDomainManager,self).get_query_set().filter(region = 3)

class StarDomainManager(models.Manager):
    """
    .com域名管理
    """
    def get_query_set(self):
        return super(StarDomainManager,self).get_query_set().filter(stared = True)

class ExpiringDomainManager(models.Manager):
    """
    .com域名管理
    """
    def get_query_set(self):
        date_after_seven_days_from_now = datetime.datetime.now().date() + datetime.timedelta(7)
        return super(ExpiringDomainManager,self).get_query_set().filter(expiration_date__lte = date_after_seven_days_from_now)

class UnregisteredDomainManager(models.Manager):
    """
    未注册的域名管理
    """
    def get_query_set(self):
        return super(UnregisteredDomainManager,self).get_query_set().filter(register_status = 1)

class UnCertainDomainManager(models.Manager):
    """
    未确定信息有效的域名管理
    """
    def get_query_set(self):
        return super(UnCertainDomainManager,self).get_query_set().filter(register_status = 2)

class Domain(models.Model):
    """
    已注册域名信息
    名称/注册商/Whois服务器/引用网址/Whois信息更新日期
    域名创建日期/域名过期日期/NameServer/状态/联系邮箱/
    域名评级/是否被关注/备注(如拼音)
    """
    REGISTER_STATUS = (
        (0,"Registered"),
        (1,"UnRegistered"),
        (2,"UnCertain"),
    )

    REGIONS = (
        (0,".com"),
        (1,".net"),
        (2,".org"),
        (3,".info"),
    )

    name = models.CharField(max_length=500)
    registrar = models.CharField(max_length=500,blank=True)
    whois_server = models.CharField(max_length=500,blank=True)
    referral_url =  models.CharField(max_length=500,blank=True)
    updated_date = models.DateField(null=True,blank=True)
    creation_date = models.DateField(null=True,blank=True)
    expiration_date = models.DateField(null=True,blank=True)
    name_servers = models.CharField(max_length=300,blank=True)
    status = models.CharField(max_length=500,blank=True)
    emails = models.CharField(max_length=500,blank=True)
    register_status = models.IntegerField(null=True,choices=REGISTER_STATUS)
    region = models.IntegerField(null=True,choices=REGIONS)
    level = models.IntegerField(null=True,blank=True)
    stared = models.BooleanField(blank=True,default=False)
    comment = models.TextField(blank=True)

    #Manager
    domains = models.Manager()
    com_domains = ComDomainManager()
    net_domains = NetDomainManager()
    org_domains = OrgDomainManager()
    info_domains = InfoDomainManager()
    stared_domains = StarDomainManager()
    expiring_domains = ExpiringDomainManager()
    unregistered_domains = UnregisteredDomainManager()
    uncertain_domains = UnCertainDomainManager()

    def __str__(self):
        return "%s" % self.name

    def __unicode__(self):
        return u'%s' % self.name

    def _get_pinyin(self):
        if self.comment:
            pinyin_match = re.search(r"pinyin:([\w']+)",self.comment)
            if pinyin_match:
                return u"%s" % pinyin_match.groups()[0]
        return None

    def _get_cihui(self):
        if self.comment:
            cihui_match = re.search(r"cihui:([^\t]+)",self.comment)
            if cihui_match:
                return u"%s" % cihui_match.groups()[0]
        return None


    def _get_details(self):
        """
        返回此域名的详细信息
        """
        return u"Name:%s\nRegistrar:%s\nCreate Date:%s\nExpired Date:%s\nStatus:%s\nEmails:%s\nName Servers:%s\nWhois Server:%s\nReferral:%s\nUpdate Date:%s\nLevel:%s\nStar:%s\nComment:%s\n" % (self.name,self.registrar,self.creation_date,self.expiration_date,self.status,self.emails,self.name_servers,self.whois_server,self.referral_url,self.updated_date,self.level,self.stared,self.comment)

    detail = property(_get_details)
    pinyin = property(_get_pinyin)
    cihui = property(_get_cihui)
